package io;

import java.io.*;

/**
 * 包装流-BufferedInputStream
 */
public class BufferedInputStreamAndBufferedOutputStream {

    public static void main(String[] args) {
        /*
            1)BufferedInputStream 是字节流，在创建时，会创建一个内部缓冲区数组
            2)BufferedOutputStream 是字节流，可以将多个字节写入底层输入流中，而不必对每次字节写入调用底层系统
            完成二进制文件的拷贝
         */

        String srcFilePath = "D:\\IdeaCode\\递归.jpg";
        String destFilePath = "D:\\IdeaCode\\javaStudy\\递归.jpg";

        //创建BufferedOutputStream 对象 BufferedInputStream 对象
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            bis = new BufferedInputStream(new FileInputStream(srcFilePath));
            bos = new BufferedOutputStream(new FileOutputStream(destFilePath));

            //循环读取文件
            byte[] buf = new byte[1024];
            int readLen = 0;
            while((readLen = bis.read(buf)) != -1) {
                //写入
                bos.write(buf, 0, readLen);
            }
            System.out.println("文件拷贝完成");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
